<template>
  <div class="p-d">
    <div class="b-f p-d">
      <SearchComp :searchForm="searchForm" :searchData="queryString" :searchAction="searchAction"></SearchComp>
      <div class="btnStyle flex-a-c p-t">
        <el-button type="primary" size="small" @click="add" class="m-r-10">新增</el-button>
        <el-upload ref="uploadRefXls" class="upload-demo m-r-10" action="#" :limit="1" :on-change="handleChangeXls" :auto-upload="false"
          :show-file-list="false" :on-exceed="handleExceedXls" accept=".xls,.xlsx">
          <template #trigger>
            <el-button type="warning" size="small">批量导入</el-button>
          </template>
        </el-upload>
        <el-button type="warning" size="small" @click="handleDownload">批量导出</el-button>
        <el-button type="danger" size="small" @click="dels">批量删除</el-button>
      </div>
      <div class="m-t">
        <TableCompEle class="m-t" ref="multipleTable" height="50vh" :tableData="tableData" :tableConfig="tableConfig" :tableAction="tableAction"
          :isSelect="true" :isIndex="true" :isShowPage="true" :queryParams="queryParams" @getPagination="getList"
          @handleSelectionChange="handleSelectionChange"></TableCompEle>
      </div>
    </div>
    <AddComp ref="addRef" @close="getList" />
  </div>
</template>

<script>
import AddComp from './common/add.vue'
export default {
  components: { AddComp },
  data() {
    return {
      activeName: 'all',
      queryParams: {
        total: 10,
        currentPage: 1,
        pageSize: 10
      },
      tableData: [
        {
          "unitName": "军事医学科研所实验中心",
          "largeName": "解放军总后勤部",
          "year": '2023',
          "openRate": 90.3,
          "sharingRate": 85.7,
          "serviceRevenue": 600000,
          "yearHours": 15000
        },
        {
          "unitName": "军队分子生物学实验室",
          "largeName": "南部战区科研机构",
          "year": '2023',
          "openRate": 87.9,
          "sharingRate": 78.5,
          "serviceRevenue": 450000,
          "yearHours": 13000
        },
        {
          "unitName": "军医大学病理研究中心",
          "largeName": "东部战区医科大学",
          "year": '2023',
          "openRate": 92.1,
          "sharingRate": 82.1,
          "serviceRevenue": 720000,
          "yearHours": 18000
        },
        {
          "unitName": "部队卫生装备研发部",
          "largeName": "国防大学",
          "year": '2023',
          "openRate": 84.5,
          "sharingRate": 73.8,
          "serviceRevenue": 500000,
          "yearHours": 12000
        },
        {
          "unitName": "军事临床医学研究所",
          "largeName": "中部战区总医院",
          "year": '2023',
          "openRate": 89.8,
          "sharingRate": 80.9,
          "serviceRevenue": 650000,
          "yearHours": 16000
        },
        {
          "unitName": "军事预防医学研究中心",
          "largeName": "军事科学院",
          "year": '2023',
          "openRate": 91.2,
          "sharingRate": 84.3,
          "serviceRevenue": 780000,
          "yearHours": 20000
        },
        {
          "unitName": "军队生物医学工程室",
          "largeName": "战略支援部队",
          "year": '2023',
          "openRate": 88.1,
          "sharingRate": 79.5,
          "serviceRevenue": 550000,
          "yearHours": 14000
        },
        {
          "unitName": "军队药理学与毒理学实验室",
          "largeName": "联勤保障部队",
          "year": '2023',
          "openRate": 93.5,
          "sharingRate": 88.2,
          "serviceRevenue": 900000,
          "yearHours": 22000
        },
        {
          "unitName": "军队急救医学装备研发部",
          "largeName": "空军后勤部",
          "year": '2023',
          "openRate": 86.3,
          "sharingRate": 76.9,
          "serviceRevenue": 480000,
          "yearHours": 11000
        },
        {
          "unitName": "军队放射医学研究中心",
          "largeName": "火箭军医科大学",
          "year": '2023',
          "openRate": 90.0,
          "sharingRate": 83.1,
          "serviceRevenue": 700000,
          "yearHours": 17000
        }
      ],
      tableConfig: [
        {
          prop: 'unitName',
          label: '管理单位名称'
        },
        {
          prop: 'largeName',
          label: '所属大单位'
        },
        {
          prop: 'year',
          label: '评价考核年度'
        },
        {
          prop: 'openRate',
          label: '开放率（%）'
        },
        {
          prop: 'sharingRate',
          label: '共享率（%）'
        },
        {
          prop: 'serviceRevenue',
          label: '服务收入（万元）'
        },
      ],
      tableAction: {
        align: () => 'center',
        width: () => String(170),
        operations: [
          {
            label: '查看',
            isLink: true,
            type: 'primary',
            method: row => {
              this.$refs.addRef.show({
                title: '查看评价考核',
                isLook: 'look',
                data: row
              })
            }
          },
          {
            label: '修改',
            isLink: true,
            type: 'primary',
            method: row => {
              this.$refs.addRef.show({
                title: '修改评价考核',
                isLook: 'edit',
                data: row
              })
            }
          },
          {
            label: '删除',
            isLink: true,
            type: 'danger',
            method: row => {
              this.deleteFun([row.id])
            }
          }
        ]
      },
      queryString: {
        title: '',
        id: ''
      },
      searchForm: {
        changeMethod: () => {
          this.queryParams.currentPage = 1
          this.getList()
        },
        formData: [
          {
            type: 'Input',
            label: '管理单位名称',
            prop: 'id',
            clearable: true,
            icon: 'el-icon-search',
            placeholder: '请输入'
          },
          {
            type: 'Input',
            label: '所属大单位',
            prop: 'title',
            clearable: true,
            icon: 'el-icon-search',
            placeholder: '请输入'
          },
          {
            type: 'Input',
            label: '年度',
            prop: 'title',
            clearable: true,
            icon: 'el-icon-search',
            placeholder: '请输入'
          },
        ]
      },
      searchAction: [
        {
          label: '查询',
          type: 'primary',
          handle: () => {
            this.queryParams.currentPage = 1
            this.$refs.multipleTable.clearSelection()
            this.getList()
          }
        },
        {
          label: '重置',
          type: 'primary',
          plain: true,
          handle: () => {
            this.queryString = {
              title: '',
              id: ''
            }
            this.queryParams.currentPage = 1
            this.getList()
          }
        }
      ]
    }
  },
  methods: {
    add() {
      this.$refs.addRef.show({
        title: '新建评价考核',
        isLook: 'add'
      })
    },
    getList() { },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleDownload() {
      if (this.multipleSelection.length == 0) {
        this.$message({
          type: 'warning',
          message: '请选择要导出的数据'
        })
        return
      }
      const ids = []
      this.multipleSelection.forEach(items => {
        ids.push(items.id)
      })
      this.downloadFun(ids)
    },
    downloadFun(ids) {
      console.log('%c [ ids ]-283', 'font-size:13px; background:pink; color:#bf2c9f;', ids)
      this.$confirm(`确定要导出吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // downloadMultiple(ids)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    dels() {
      if (this.multipleSelection.length == 0) {
        this.$message({
          type: 'warning',
          message: '请选择要删除的数据'
        })
        return
      }
      const ids = []
      this.multipleSelection.forEach(items => {
        ids.push(items.articleId)
      })
      this.deleteFun(ids)
    },
    deleteFun(ids) {
      console.log(
        '%c [ ids ]-216',
        'font-size:13px; background:pink; color:#bf2c9f;',
        ids
      )
      this.$confirm(`确定要删除吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    // xls
    handleChangeXls(file) {
      const _file = file?.raw ?? file
      const formData = new FormData()
      formData.append('file', _file)
      formData.append('pathType', file.name.split('.').pop())
    },
    // 多次上传覆盖
    handleExceedXls(file) {
      this.$refs.uploadRefXls.clearFiles()
      this.handleChangeXls(file[0])
    }
  },
}
</script>

<style lang="less" scoped>
</style>